package cn.itsource.pojo.ro;

import cn.hutool.core.bean.BeanUtil;
import cn.itsource.constants.RConst;
import cn.itsource.pojo.domain.DriverAuthMaterial;
import lombok.Data;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Data
public class DriverAuthMaterialRo {
    private Idcard idcard;
    private Contact contact;
    private Drcard drcard;
    private String driverId;

    @Data
    public class Idcard{
        private String name;//
        private String idNumber;//
        private String gender;//
        private String idcardAddress;//
        private String birthday;//
        private String idCardExpire;//
        private String idcardFront;//
        private String idcardBack;//
        private String idcardHolding;//
    }
    @Data
    public class Contact{
        private String phone;//
        private String email;//
        private String mailAddress;
        private String contactName;//
        private String contactPhone;//
    }

    @Data
    public class Drcard{
        private String carClass;//
        private String drcardIssueDate;//
        private String drcardExpire;//
        private String drcardFront;//
        private String drcardBack;//
        private String drcardHolding;//
    }
    public static DriverAuthMaterial toDriverAuthMaterial(DriverAuthMaterialRo driverInfoRo){
        DriverAuthMaterial driverAuthMaterial = new DriverAuthMaterial();
        driverAuthMaterial.setId(Long.valueOf(driverInfoRo.getDriverId()));
        driverAuthMaterial.setDriverId(Long.valueOf(driverInfoRo.getDriverId()));
        //设置过期时间
        Date idexpire = getExpire(driverInfoRo.getIdcard().getIdCardExpire());
        driverAuthMaterial.setIdcardExpire(idexpire);
        Date drexpire = getExpire(driverInfoRo.getDrcard().getDrcardExpire());
        driverAuthMaterial.setDrcardExpire(drexpire);
        //设置生日
        driverAuthMaterial.setBirthday(getBirth(driverInfoRo.getIdcard().getBirthday()));
        //设置审核状态
        driverAuthMaterial.setRealAuthStatus(RConst.Num.INT_0);
        driverAuthMaterial.setCreateTime(new Date());
        BeanUtil.copyProperties(driverInfoRo.getIdcard(),driverAuthMaterial);
        BeanUtil.copyProperties(driverInfoRo.getDrcard(),driverAuthMaterial);
        BeanUtil.copyProperties(driverInfoRo.getContact(),driverAuthMaterial);
        return driverAuthMaterial;
    }

    /**
     * 格式化日期
     * @param idCardExpire
     * @return
     */
    private static Date getExpire(String idCardExpire) {
        try {
            String idExpire = idCardExpire.substring(
                    idCardExpire.indexOf(RConst.Str.DOT)+RConst.Num.INT_1);
            SimpleDateFormat format = new SimpleDateFormat(RConst.DataFormat.YYYY_MM_DD);
            SimpleDateFormat format1 = new SimpleDateFormat(RConst.DataFormat.YYYYMMDD);
            Date parse = format1.parse(idExpire);
            String format3 = format.format(parse);
            return format.parse(format3);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    private static Date getBirth(String birth) {
        try {
        SimpleDateFormat format = new SimpleDateFormat(RConst.DataFormat.YYYY_MM_DD);
        return format.parse(birth);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
